<?php

/**
 * Created by PhpStorm.
 * User: jimmyhsu
 * Date: 2016/10/4
 * Time: 下午11:46
 */
class SMSVerify
{
    public $app_key = '179f96bf79570';
    public $api = 'https://webapi.sms.mob.com/sms/verify';
    public function verify($phone, $code){
        $response = $this->postRequest($this->api, array(
            'appkey' => $this->app_key,
        'phone' => $phone,
        'zone' => '86',
        'code' => $code,
        ) );
//        echo $response;
//        $arr = array ('status'=>200);
//        $response=json_encode($arr);
//        echo $response;
        $obj = json_decode($response);
        if ($obj->{'status'} == 200) {
            return true;
        } else {
            return false;
        }
    }
    function postRequest($api, array $params = array(), $timeout = 300) {
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $api );
        // 以返回的形式接收信息
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        // 设置为POST方式
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
        // 不验证https证书
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
        curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
        curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
            'Accept: application/json',
        ) );
        // 发送数据
        $response = curl_exec( $ch );
        // 不要忘记释放资源
        curl_close( $ch );
        return $response;
    }

}